package example;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class NewsGain {
	public static void main(String[] args) {
		String httpUrl = "http://apis.baidu.com/showapi_open_bus/extract/extract";
		String httpArg = "url=http://news.sina.com.cn/china/xlxw/2016-09-11/doc-ifxvukhv8111216.shtml";
		String jsonResult = request(httpUrl, httpArg);
		System.out.println(jsonResult);

	}
		/**
		 * @param urlAll
		 *            :请求接口
		 * @param httpArg
		 *            :参数
		 * @return 返回结果
		 */
		public static String request(String httpUrl, String httpArg) {
		    BufferedReader reader = null;
		    String result = null;
		    StringBuffer sbf = new StringBuffer();
		    httpUrl = httpUrl + "?" + httpArg;

		    try {
		        URL url = new URL(httpUrl);
		        HttpURLConnection connection = (HttpURLConnection) url
		                .openConnection();
		        connection.setRequestMethod("GET");
		        // 填入apikey到HTTP header
		        connection.setRequestProperty("apikey", "f999c928af06bf15c04f9d57913bf24c");
		        connection.connect();
		        InputStream is = connection.getInputStream();
		        reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
		        String strRead = null;
		        while ((strRead = reader.readLine()) != null) {
		            sbf.append(strRead);
		            sbf.append("\r\n");
		        }
		        reader.close();
		        result = sbf.toString();
		    } catch (Exception e) {
		        e.printStackTrace();
		    }
		    return result;
		}


}
